//David Wasser

/* DefStackRoutine is the MovementRoutine that controls players
 * in an defensive stack (DefStack) strategy in which the players
 * are each assigned a mark to guard that they follow around the
 * field.
 */
import javax.vecmath.Point3d;
import javax.vecmath.Tuple3d;
import javax.vecmath.Vector3d;
public class DefStackRoutine extends PlayerRoutine{
	Player mark;
	boolean catching = false;
	Vector3d test;
	
	/** Creates a new DefStackRoutine for the input Player at the position with the given mark
	 ** under the given DefStack.
	 **/
	public DefStackRoutine(Player setPlayer, Point3d position, Player mark, DefStack strategy){
		this.mark = mark;
		strat = strategy;
		player = setPlayer;
		player.setMovementRoutine(this);
		point = position;
		vec = new Vector3d((player.team.endZone == Team.RIGHT?1:-1),0,0);
		goal = (Point3d)point.clone();
	}//End DefStackRoutine()
	
	/** Step calculates the next point that the person is moving to, and
	 ** the player's velocity vector.
	 **/
	public Tuple3d[] step(){
		if(strat.starting)
			return new Tuple3d[] {point, vec};
		if(mark != null)
			if(!airborn){
				if(catching){
					if(field.disc.isHeld())
						catching = false;
				}else
					if(((DefStack)strat).force == ((DefStack)strat).UP)
						goal = new Point3d(mark.position.x, mark.position.y-2, 0);
					else
						goal = new Point3d(mark.position.x, mark.position.y+2, 0);
				vec = new Vector3d((Point3d)(goal.clone()));
				vec.sub(point);
				vec.scale(player.speed*field.dt/vec.length());
				test = new Vector3d();
				test.sub(point, goal);
				if(test.length() < vec.length())
					vec.sub(goal, point);
				point.add(vec);
			}//End if
		return new Tuple3d[] {point, vec};
	}//End step()
	
	/** Tells the DefStackRoutine where to move the Player into
	 ** the stack.
	 **/
	public void setField(Field gameField){
		field = gameField;
	}//End setField()
}//End DefStackRoutine